<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="审计项目" prop="fAuditname">
        <el-input
          v-model="queryParams.fAuditname"
          placeholder="请输入审计项目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审计结果" prop="fAuditresult">
        <el-input
          v-model="queryParams.fAuditresult"
          placeholder="请输入审计结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="fAuditpeople">
        <el-input
          v-model="queryParams.fAuditpeople"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:audit:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:audit:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:audit:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:audit:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="auditList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目id" align="center" prop="fAuditid" />
      <el-table-column label="审计项目" align="center" prop="fAuditname" />
      <el-table-column label="业务流程" align="center" prop="fAuditflow" />
      <el-table-column label="审计结果" align="center" prop="fAuditresult" />
      <el-table-column label="内容类型" align="center" prop="fAudittype" />
      <el-table-column label="大小" align="center" prop="fAuditsize" />
      <el-table-column label="创建人" align="center" prop="fAuditpeople" />
      <el-table-column label="创建时间" align="center" prop="fAudittime" />
      <el-table-column label="修改时间" align="center" prop="fAuditretime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate1(scope.row)"
            v-hasPermi="['system:audit:edit']"
          >审计</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改审计和合规性对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审计项目" prop="fAuditname">
          <el-input v-model="form.fAuditname" placeholder="请输入审计项目" />
        </el-form-item>
        <el-form-item label="业务流程" prop="fAuditflow">
          <el-input v-model="form.fAuditflow" placeholder="请输入业务流程" />
        </el-form-item>
        <el-form-item label="创建人" prop="fAuditpeople">
          <el-input v-model="form.fAuditpeople" placeholder="请输入创建人" />
        </el-form-item>
        <el-col :span="1.5">
          <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple
            :limit="3"
            :on-exceed="handleExceed"
            :file-list="fileList">
            <el-button size="small" type="primary">点击上传文件</el-button>
          </el-upload>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 审计合规性对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审计项目" prop="fAuditname">
          <el-input v-model="form.fAuditname"/>
        </el-form-item>
        <el-form-item label="业务流程" prop="fAuditflow">
          <el-input v-model="form.fAuditflow"/>
        </el-form-item>
        <el-form-item label="创建人" prop="fAuditpeople">
          <el-input v-model="form.fAuditpeople"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="fAudittime">
          <el-input v-model="form.fAudittime"/>
        </el-form-item>
        <el-form-item label="修改时间" prop="fAuditretime">
          <el-input v-model="form.fAuditretime"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">通 过</el-button>
        <el-button type="primary" @click="submitForm2">未 通 过</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAudit, getAudit, delAudit, addAudit, updateAudit } from "@/api/system/audit";

export default {
  name: "Audit",
  data() {
    return {
      fileList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审计和合规性表格数据
      auditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fAuditid: null,
        fAuditname: null,
        fAuditflow: null,
        fAuditresult: null,
        fAudittype: null,
        fAuditpeople: null,
        fAudittime: null,
        fAuditretime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fAuditname: [
          { required: true, message: "审计项目不能为空", trigger: "blur" }
        ],
        fAuditflow: [
          { required: true, message: "业务流程不能为空", trigger: "blur" }
        ],
        fAudittype: [
          { required: true, message: "内容类型不能为空", trigger: "change" }
        ],
        fAuditpeople: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getCurrentTime() {
      //获取当前时间并打印
      var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth()+1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy+'/'+mm+'/'+dd+' '+hh+':'+mf+':'+ss;
      return  _this.gettime
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    /** 查询审计和合规性列表 */
    getList() {
      this.loading = true;
      listAudit(this.queryParams).then(response => {
        this.auditList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fAuditid: null,
        fAuditname: null,
        fAuditflow: null,
        fAuditresult: null,
        fAudittype: null,
        fAuditsize: null,
        fAuditpeople: null,
        fAudittime: null,
        fAuditretime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fAuditid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审计和合规性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fAuditid = row.fAuditid || this.ids
      getAudit(fAuditid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改审计和合规性";
      });
    },
    /** 审计按钮操作 */
    handleUpdate1(row) {
      this.reset();
      const fAuditid = row.fAuditid || this.ids
      getAudit(fAuditid).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title = "审计合规性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fAuditid != null) {
             this.form.fAuditretime = this.getCurrentTime();
            updateAudit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
           } else {
            this.form.fAudittime = this.getCurrentTime();
            addAudit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 审计提交按钮 */
    submitForm1() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fAuditid != null) {
            this.form.fAuditresult = "审计通过";
            updateAudit(this.form).then(response => {
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm2() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fAuditid != null) {
            this.form.fAuditresult = "审计未通过";
            updateAudit(this.form).then(response => {
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fAuditids = row.fAuditid || this.ids;
      this.$modal.confirm('是否确认删除审计和合规性编号为"' + fAuditids + '"的数据项？').then(function() {
        return delAudit(fAuditids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/audit/export', {
        ...this.queryParams
      }, `audit_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
